<?php

namespace app\store\controller\spike;

use app\store\controller\BaseController;
use app\store\model\SpikeGoods;
use app\store\service\GoodsCategoryService;
use app\store\service\SpikeGoodsService;
use app\store\service\GoodsService;
use think\facade\View;
use think\exception\ValidateException;

class GoodsController extends BaseController
{

    /**
     *
     * 获取秒杀商品列表
     * */
    public function index()
    {   
        $param = $this->request->get();
        $goodsService = new SpikeGoodsService();
        $list = $goodsService->getSpikeGoodsList($param);



        return View::fetch('',['list' => $list]);
    }
    
    public function create()
    {   
        $id = $this->request->get('id/d');

        $goodsService = new GoodsService();
        $row = $goodsService->findOptionGoods($id);


        return View::fetch('',['row' => $row]);
    }

    public function option()
    {
        $param = $this->request->param();

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption(true);

        $spikeGoodsService = new SpikeGoodsService();
        $list = $spikeGoodsService->getSpikeGoodsOptions($param);


        View::engine()->layout(false);
  
        return View::fetch('',['options' => $options,'list' => $list]);
    }


    public function edit()
    {   
        $id = $this->request->get('id/d');

        $goodsService = new SpikeGoodsService();
        $row = $goodsService->findSpikeGoods($id);



        return View::fetch('',['row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();

        try {
            validate('SpikeGoodsValidate')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $goodsService = new SpikeGoodsService();
        $id =  $goodsService->saveSpikeGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);

    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            validate('GoodsValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        SpikeGoods::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function update()
    {
        $param = $this->request->post();

        try {
            validate('SpikeGoodsValidate.edit')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $goodsSpikeService = new SpikeGoodsService();
        $id =  $goodsSpikeService->updateSpikeGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);
    }


    public function delete()
    {
        $id = $this->request->get('id/d');
        $result = SpikeGoods::destroy($id);

        return $result ?  json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }
}